package com.beverages.messages.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beverages.messages.client.CustomerMessagesClient;
import com.beverages.messages.client.MerchantOrdersClient;
import com.beverages.messages.entity.CancelOrderReply;
import com.beverages.messages.entity.dto.CancelOrderReplyDTO;
import com.beverages.messages.entity.dto.intermediate.OrdersDTO;
import com.beverages.messages.entity.intermediate.CancelOrderRequest;
import com.beverages.messages.mapper.CancelOrderReplyMapper;
import com.beverages.messages.service.ICancelOrderReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

@Service
public class CancelOrderReplyServiceImpl extends ServiceImpl<CancelOrderReplyMapper, CancelOrderReply> implements ICancelOrderReplyService {

    @Autowired
    private MerchantOrdersClient merchantOrdersClient;

    @Autowired
    private CustomerMessagesClient customerMessagesClient;

    @Override
    public CancelOrderReplyDTO convertCancelOrderReplyEntityToCancelOrderReplyDTO(CancelOrderReply cancelOrderReply){
        CancelOrderReplyDTO dto = new CancelOrderReplyDTO();

        dto.setReplyId(cancelOrderReply.getId());

        dto.setRequestId(cancelOrderReply.getRequestId());
        CancelOrderRequest cancelOrderRequest = customerMessagesClient.getCancelOrderRequestById(cancelOrderReply.getRequestId()).getData();
        dto.setRequestContent(cancelOrderRequest.getContent());
        dto.setRequestTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cancelOrderRequest.getRequestTime()));

        dto.setOrderNumber(cancelOrderRequest.getOrderNumber());
        OrdersDTO oneOrderDTO = merchantOrdersClient.findOneByOrderNumber(cancelOrderRequest.getOrderNumber()).getData();
        dto.setMilkteaId(oneOrderDTO.getMilkteaId());
        dto.setMilkteaName(oneOrderDTO.getMilkteaName());
        dto.setFromCustomerId(oneOrderDTO.getCustomerId());
        dto.setFromCustomerName(oneOrderDTO.getCustomerName());
        dto.setFromCustomerPhone(oneOrderDTO.getCustomerPhone());

        dto.setReplyContent(cancelOrderReply.getContent());
        dto.setDecision(cancelOrderReply.getDecision());
        dto.setReplyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cancelOrderReply.getReplyTime()));

        return dto;
    }
}